


import axios from '../js_sdk/xtshadow-axios/axios.min'
import store from '../store'
import {
	paramsToStr,
	currentPage,
	toast
} from './tools'
import Cache from './cache'
import {setsign} from './sign'
import {
	TOKEN,
	DISTRIBUTION
} from '../config/cachekey'
import {baseURL} from '../config/app'
import {
	toLogin
} from './login'
import {router} from '../router'
let index = 0;

function checkParams(params) {
	if (typeof params != 'object') return params
	for (let key in params) {
		const value = params[key];
		if (value === null || value === undefined || value === "") {
			delete params[key];
		}
	}
	return params;
}



const service = axios.create({
	baseURL: baseURL + '/api/',
	timeout: 10000,
	header: {
		'content-type': 'application/json'
	},

});


// request拦截器
service.interceptors.request.use(
	config => {
		config.data = checkParams(config.data)
		config.params = checkParams(config.params)
		if (config.method == 'GET') {
			config.url += paramsToStr(config.params)
		}
		let distributionInfo = Cache.get(DISTRIBUTION)
		config.header.distributionid = config.header.distributionid || store.getters.distribution.id| distributionInfo.id | 0,
		config.header.token =  config.header.token || store.getters.token || '',
		config.header.sign = setsign(config.header.token,config.params,config.data)
		// console.log('==/utils/request.js==发送http请求==拦截器====config=====');
		// console.log(config);
		return config
	},
	error => {
		// Do something with request error
		console.log(error) // for debug
		Promise.reject(error)
	}
)

// response 拦截器
service.interceptors.response.use(
	async (response) => {
		
			if (response.data) {
				const {
					code,
					show,
					msg
				} = response.data;
				if (code == 0 && show) {
					toast({
						title: msg,
					})
				} else if (code == -1) {
					store.commit('logout')
					toLogin()
				} else if(code == 301) {
					// 返回上一页
					toast({
						title: msg,
					},{
						tab: 3,
						url: 1
					})
				}
			}

			return Promise.resolve(response.data)
		},
		error => {
			uni.showToast({
				title: "系统错误",
				icon: "none"
			})
			console.log(error)
			console.log('err' + error) // for debug
			return Promise.reject(error)
		}
)

export default service
